package com.baboba.rummy.game;

import java.util.LinkedList;
import java.util.List;

import com.baboba.rummy.cards.Card;
import com.baboba.rummy.cards.CardBitMap;
import com.baboba.rummy.cards.Color;
import com.baboba.rummy.cards.ColorCard;

public class IceBreaker {
	
	public static List<LinkedList<Card>> awkwardBreak(CardBitMap bitMap){
		List<LinkedList<Card>> result = new LinkedList<LinkedList<Card>>();
		CardBitMap bitMapClone = bitMap;
		
		List<LinkedList<Card>> verticalCards = getAllVerticalAvailableCards(bitMapClone);
		result.addAll(verticalCards);
		
		List<LinkedList<Card>> horizontalCards = getAllHorizontalAvailableCards(bitMapClone);
		result.addAll(horizontalCards);
		
		finalProccessIceBreak(result);
		
		return result;
	}
	
	private static void finalProccessIceBreak(List<LinkedList<Card>> result) {
		//TODO 
	}

	public static List<LinkedList<Card>> getAllHorizontalAvailableCards(CardBitMap bitMap){
		List<LinkedList<Card>> result = new LinkedList<LinkedList<Card>>();
		Color[] colors = Color.values();
		
		for (Color color:colors){
			LinkedList<Card> cards = new LinkedList<Card>();
			for (int i=13;i>=1;i--){
				if (bitMap.getCardBitMap(color.ordinal(), i)>0)
					cards.add(new ColorCard(i, color));
				else{
					if (cards.size()>=3){
						result.add(cards);
						cards = new LinkedList<Card>();
					}else
						cards.clear();
				}
			}
			
			cards.clear();
			for (int i=13;i>=1;i--){
				if (bitMap.getCardBitMap(color.ordinal(), i)>1)
					cards.add(new ColorCard(i, color));
				else{
					if (cards.size()>=3){
						result.add(cards);
						cards = new LinkedList<Card>();
					}else
						cards.clear();
				}
			}
		}
		
		return result;
	}
	
	public static List<LinkedList<Card>> getAllVerticalAvailableCards(CardBitMap bitMap){
		List<LinkedList<Card>> result = new LinkedList<LinkedList<Card>>();
		Color[] colors = Color.values();
		
		//from big to small
		for (int i=13;i>=1;i--){
			//iterate every color
			
			LinkedList<Card> cards = new LinkedList<Card>();
			for (Color color:colors){
				if (bitMap.getCardBitMap(color.ordinal(), i)>0)
					cards.add(new ColorCard(i, color));
			}
			
			if (cards.size()>=3)
				result.add(cards);
			
			cards = new LinkedList<Card>();
			for (Color color:colors){
				if (bitMap.getCardBitMap(color.ordinal(), i)>1)
					cards.add(new ColorCard(i, color));
			}
			
			if (cards.size()>=3)
				result.add(cards);
		}
		
		return result;
	}
	
	private static List<LinkedList<Card>> checkHorizontal(CardBitMap bitMap){
		//removeCards();
		return null;
	}
	
	private static List<LinkedList<Card>> checkVertical(CardBitMap bitMap){
		/*Color[] colors = Color.values();
		
		//from big to small
		for (int i=13;i>=1;i--){
			//iterate every color
			for (Color color:colors){
				if (bitMap.getCardBitMap(color.ordinal(), i)>0)
					//result++;
			}
		}
		
		//removeCards();
		*/
		return null;
	}
	
	private static boolean hasLeftSequence(CardBitMap bitMap, Color color, int number){
		if (number<3)
			return false;
		
		return bitMap.getCardBitMap(color.ordinal(), number)>0
				&& bitMap.getCardBitMap(color.ordinal(), number-1)>0
				&& bitMap.getCardBitMap(color.ordinal(), number-2)>0;
	}
	
	private static int hasVerticalSame(CardBitMap bitMap, int number){
		Color[] colors = Color.values();
		int result = 0;
		
		for (Color color:colors){
			if (bitMap.getCardBitMap(color.ordinal(), number)>0)
				result++;
		}
		
		return result;
	}
	
	private static void removeCards(CardBitMap bitMap, List<Card> cards){
		for (Card card:cards){
			if (card instanceof ColorCard){
				ColorCard colorCard = (ColorCard)card;
				bitMap.decrease(colorCard.getColor().ordinal() , card.getNumber());
			}
		}
	}
}
